package com.google.project.server.entity;

import java.io.Serializable;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.project.shared.CryptString;
import com.google.project.shared.UsersDTO;

@SuppressWarnings("serial")
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Users implements Serializable {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String userId;
	@Persistent
	private String userCode;
	@Persistent
	private String userName;
	@Persistent
	private String password;
	@Persistent
	private String email;
	
	@Persistent
	private String fullName;
	@Persistent
	private Long birthday;
	@Persistent
	private String hometown;
	@Persistent
	private int sex;
	@Persistent
	private String idCard;
	@Persistent
	private String phoneNumber;
	@Persistent
	private String address;
	@Persistent
	private String specializeID;
	@Persistent
	private String specializeName;
	@Persistent
	private int experient;
	@Persistent
	private Long joinDate;
	@Persistent
	private String status;
	@Persistent
	private String description;
	@Persistent
	private String currentProjID;
	@Persistent
	private Long userGroup;

	public Users() {
	}
	CryptString cryptPass=new CryptString();
	public Users(UsersDTO userDTO){
		this.userCode = userDTO.getUserCode();
		this.userName = userDTO.getUserName();
		CryptString cryptPass=new CryptString();
		this.password =cryptPass.encryptPassword( userDTO.getPassword());
		this.email = userDTO.getEmail();
		this.fullName = userDTO.getFullName();
		this.birthday = userDTO.getBirthday();
		this.hometown = userDTO.getHometown();
		this.sex = userDTO.getSex();
		this.idCard=userDTO.getIdCard();
		this.phoneNumber = userDTO.getPhoneNumber();
		this.address = userDTO.getAddress();
		this.specializeID = userDTO.getSpecializeID();
		this.specializeName=userDTO.getSpecializeName();
		this.experient = userDTO.getExperient();
		this.joinDate = userDTO.getJoinDate();
		this.status = userDTO.getStatus();
		this.currentProjID = userDTO.getCurrentProjID();
		this.userGroup = userDTO.getUserGroup();
		this.description = userDTO.getDescription();
	}
	
	public UsersDTO toDTO() {
		UsersDTO data = new UsersDTO();
		data.setUserId(userId);
		data.setUserCode(userCode);
		data.setUserName(userName);
		data.setPassword(password);
		data.setEmail(email);
		data.setFullName(fullName);
		data.setBirthday(birthday);
		data.setHometown(hometown);
		data.setSex(sex);
		data.setIdCard(idCard);
		data.setPhoneNumber(phoneNumber);
		data.setAddress(address);
		data.setSpecializeID(specializeID);
		data.setSpecializeName(specializeName);
		data.setExperient(experient);
		data.setJoinDate(joinDate);
		data.setStatus(status);
		data.setCurrentProjID(currentProjID);
		data.setUserGroup(userGroup);
		data.setDescription(description);
		return data;
	}
	
	
	/*
	 * Update function
	 */
	public void updateAcc(UsersDTO userDTO){
		this.userCode = userDTO.getUserCode();
		this.userName = userDTO.getUserName();
		this.password =cryptPass.encryptPassword( userDTO.getPassword());
		this.email = userDTO.getEmail();
		this.fullName = userDTO.getFullName();
		this.birthday = userDTO.getBirthday();
		this.hometown = userDTO.getHometown();
		this.sex = userDTO.getSex();
		this.idCard=userDTO.getIdCard();
		this.phoneNumber = userDTO.getPhoneNumber();
		this.address = userDTO.getAddress();
		this.specializeID = userDTO.getSpecializeID();
		this.specializeName=userDTO.getSpecializeName();
		this.experient = userDTO.getExperient();
		this.joinDate = userDTO.getJoinDate();
		this.status = userDTO.getStatus();
		this.currentProjID = userDTO.getCurrentProjID();
		this.userGroup = userDTO.getUserGroup();
		this.description = userDTO.getDescription();
	}
	/*
	 * Update function
	 */
	public void updateFromDTO(UsersDTO userDTO){
		this.userCode = userDTO.getUserCode();
		this.userName = userDTO.getUserName();
		this.password =userDTO.getPassword();
		this.email = userDTO.getEmail();
		this.fullName = userDTO.getFullName();
		this.birthday = userDTO.getBirthday();
		this.hometown = userDTO.getHometown();
		this.sex = userDTO.getSex();
		this.idCard=userDTO.getIdCard();
		this.phoneNumber = userDTO.getPhoneNumber();
		this.address = userDTO.getAddress();
		this.specializeID = userDTO.getSpecializeID();
		this.specializeName=userDTO.getSpecializeName();
		this.experient = userDTO.getExperient();
		this.joinDate = userDTO.getJoinDate();
		this.status = userDTO.getStatus();
		this.currentProjID = userDTO.getCurrentProjID();
		this.userGroup = userDTO.getUserGroup();
		this.description = userDTO.getDescription();
	}
	//set basic
	public void setDefaulUser(String userCode, String userName, String password,
			String fullName, Long birthday, String status,
			Long userGroup,String email){
		this.userCode=userCode;
		this.userName=userName;
		this.password=cryptPass.encryptPassword(password);
		this.fullName=fullName;
		this.birthday=birthday;
		this.status=status;
		this.userGroup=userGroup;
		this.email=email;
	}
	//getDefaultUser 
	 public static Users getDefaultUser(PersistenceManager pm) {
		    
		    String defaultAdminCode = "mg1";
		    
		    Users oneResult = null;
		    Query q = pm.newQuery(Users.class, "userCode == :useCode");
		    q.setUnique(true);
		    try {
		      oneResult = (Users) q.execute(defaultAdminCode);
		    } catch (Exception e) {
		      e.printStackTrace();
		    } finally {
		      q.closeAll();
		    }
		    return oneResult;
		  }
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public String getSpecializeName() {
		return specializeName;
	}

	public void setSpecializeName(String specializeName) {
		this.specializeName = specializeName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}


	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public Long getBirthday() {
		return birthday;
	}

	public void setBirthday(Long birthday) {
		this.birthday = birthday;
	}

	public String getHometown() {
		return hometown;
	}

	public void setHometown(String hometown) {
		this.hometown = hometown;
	}

	
	public String getSpecializeID() {
		return specializeID;
	}

	public void setSpecializeID(String specializeID) {
		this.specializeID = specializeID;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getSpecialize() {
		return specializeID;
	}

	public void setSpecialize(String specializeID) {
		this.specializeID = specializeID;
	}

	public int getExperient() {
		return experient;
	}

	public void setExperient(int experient) {
		this.experient = experient;
	}

	public Long getJoinDate() {
		return joinDate;
	}

	public void setJoinDate(Long joinDate) {
		this.joinDate = joinDate;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}


	public String getCurrentProjID() {
		return currentProjID;
	}

	public void setCurrentProjID(String currentProjID) {
		this.currentProjID = currentProjID;
	}

	public Long getUserGroup() {
		return userGroup;
	}

	public void setUserGroup(Long userGroup) {
		this.userGroup = userGroup;
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
